package com.test.mooan.labama.activity_fragment_boom.ln_boom;

import static com.test.mooan.labama.utils_boom.LoanStatusUtil.LoanStatus.No_Order;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnFragment_boom;
import com.test.mooan.labama.activity_fragment_boom.fragment_boom.DataSafetyFragment_boom;
import com.test.mooan.labama.activity_fragment_boom.fragment_boom.LnMainFragmentForOldUser_boom;
import com.test.mooan.labama.activity_fragment_boom.fragment_boom.LnMainFragment_boom;
import com.test.mooan.labama.activity_fragment_boom.fragment_boom.PersonalCenterFragment_boom;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity2;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.dialog_boom.HolidayDialog_boom;
import com.test.mooan.labama.utils_boom.LoanStatusUtil;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;

import java.util.List;
import java.util.Objects;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public class LnMainActivity_boom extends BaseLnActivity_boom {

    private ViewGroup homeLayout, dataSafetyLayout, personalCenterLayout;
    private ImageView homeIv, dataSafetyIv, personalCenterIv;
    private TextView homeTv, dataSafetyTv, personalCenterTv;

    private int fragmentShow = 1;

    private BaseLnFragment_boom homeFragment;
    private DataSafetyFragment_boom dataSafetyFragmentyoungmey;
    private PersonalCenterFragment_boom personalCenterFragment;

    public static boolean toProductPageFlag = false;
    public static Product clickProduct = null;

    public static boolean toRepayListFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_main_boom);
        pageTag = "MAIN";
        init();
    }

    private void init(){
        homeLayout = findViewById(R.id.activity_loan_main_layout_home);
        homeIv = findViewById(R.id.activity_loan_main_iv_home);
        dataSafetyLayout = findViewById(R.id.activity_loan_main_layout_data_safety);
        dataSafetyIv = findViewById(R.id.activity_loan_main_iv_data_safety);
        personalCenterLayout = findViewById(R.id.activity_loan_main_layout_personal_center);
        personalCenterIv = findViewById(R.id.activity_loan_main_iv_personal_center);
        homeTv = findViewById(R.id.activity_loan_main_tv_home);
        dataSafetyTv = findViewById(R.id.activity_loan_main_tv_data_safety);
        personalCenterTv = findViewById(R.id.activity_loan_main_tv_personal_center);

        dataSafetyFragmentyoungmey = new DataSafetyFragment_boom();
        personalCenterFragment = new PersonalCenterFragment_boom();

        homeLayout.setOnClickListener(v -> changeToHome());
        dataSafetyLayout.setOnClickListener(v -> changeToDataSafety());
        personalCenterLayout.setOnClickListener(v -> changeToPersonalCenter());

    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePage();
        if (personalCenterFragment!=null) personalCenterFragment.updateMobile();
    }

    private void updatePage() {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (homeFragment != null) fragmentTransaction.remove(homeFragment);
        if (dataSafetyFragmentyoungmey != null) fragmentTransaction.remove(dataSafetyFragmentyoungmey);
        if (personalCenterFragment != null) fragmentTransaction.remove(personalCenterFragment);

        LoanStatusUtil.LoanStatus status = ProcessInfo_boom.getInstance().gLoanStatus();
        if (!ProcessInfo_boom.getInstance().isNewUser() || status!=No_Order) homeFragment = new LnMainFragmentForOldUser_boom();
        else homeFragment = new LnMainFragment_boom();

        fragmentTransaction.add(R.id.activity_loan_main_layout_fragment, Objects.requireNonNull(homeFragment))
                .add(R.id.activity_loan_main_layout_fragment, dataSafetyFragmentyoungmey)
                .add(R.id.activity_loan_main_layout_fragment, personalCenterFragment);

        switch (fragmentShow) {
            case 2 ->
                    fragmentTransaction.show(dataSafetyFragmentyoungmey).hide(homeFragment).hide(personalCenterFragment);
            case 3 ->
                    fragmentTransaction.show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragmentyoungmey);
            default ->
                    fragmentTransaction.show(homeFragment).hide(dataSafetyFragmentyoungmey).hide(personalCenterFragment);
        }

        fragmentTransaction.commit();

        if (status==No_Order && UserInfoHelper_boom.gInstance().canShowHolidayWindow() && ProcessInfo_boom.getInstance().showHolidayWindowFlag()){
            UserInfoHelper_boom.gInstance().updateShowHolidayWindowDate();
            new HolidayDialog_boom(this,() -> HttpManager_boom.getInstance().requestUserProcess()).show();
        }

    }

    private void changeToHome() {

        homeIv.setImageResource(R.drawable.icon_tab_home_true);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.blue_boom));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray));

        getSupportFragmentManager().beginTransaction().show(homeFragment).hide(dataSafetyFragmentyoungmey).hide(personalCenterFragment).commit();

        fragmentShow = 1;
    }

    private void changeToDataSafety() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_true);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.blue_boom));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray));

        getSupportFragmentManager().beginTransaction().show(dataSafetyFragmentyoungmey).hide(homeFragment).hide(personalCenterFragment).commit();

        fragmentShow = 2;
    }

    private void changeToPersonalCenter() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_true);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.blue_boom));

        getSupportFragmentManager().beginTransaction().show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragmentyoungmey).commit();

        fragmentShow = 3;
    }

    @Override
    public void onUserProcessResponse() {

        if (isClose()) return;

        if (ProcessInfo_boom.getInstance().isNeedVerification()){
            if (CountryInfo_boom.gInstance().isCambodia()) startActivity_boom(MobileVerificationActivity.class);
            else startActivity_boom(MobileVerificationActivity2.class);
            return;
        }

        if (homeFragment instanceof LnMainFragmentForOldUser_boom){

            if (toProductPageFlag && clickProduct!=null){

                //tag2025
                Log.w("==test", clickProduct.gtProduct()+"---"+clickProduct.gtToken() );

                switch (clickProduct.productStatus){
                    case Loan_Reviewing -> {
                        Intent intent = new Intent(this, StatusReviewActivity.class);
                        intent.putExtra("product",clickProduct);
                        startActivity(intent);
                    }
                    case Order_Pending -> {
                        Intent intent = new Intent(this, StatusPendingActivity.class);
                        intent.putExtra("product",clickProduct);
                        startActivity(intent);
                    }
                    case Loan_Rejected -> {
                        Intent intent = new Intent(this, StatusRejectedActivity.class);
                        intent.putExtra("product",clickProduct);
                        startActivity(intent);
                    }
                    case Order_Pending_Repayment, Order_Overdue_Repayment -> {
                        repayProduct = clickProduct;
                        HttpManager_boom.getInstance().getRepayDetail(clickProduct.gtProduct(),clickProduct.gtToken());
                    }
                    default -> {
                        if (Constant_boom.PRODUCT.equalsIgnoreCase(clickProduct.gtProduct())) startProcess(clickProduct);
                        else {
                            applyProduct = clickProduct;
                            HttpManager_boom.getInstance().copyProcessInfo(clickProduct.gtProduct());
                        }
                    }
                }
                toProductPageFlag = false;
                clickProduct = null;
            }else HttpManager_boom.getInstance().queryProducts();

            return;

        }

        if (!ProcessInfo_boom.getInstance().isNewUser() || ProcessInfo_boom.getInstance().gLoanStatus()!=No_Order){
            updatePage();
            return;
        }

        if (homeFragment instanceof LnMainFragment_boom) {
            if (((LnMainFragment_boom) homeFragment).isRefresh){
                HttpManager_boom.getInstance().queryLoanLimit();
                ((LnMainFragment_boom) homeFragment).isRefresh = false;
            } else startProcess();
        } else updatePage();

    }

    //配置信息返回响应
    public void onAppShowInfoResponse() {
        if (personalCenterFragment != null) personalCenterFragment.updateCustomerService();
    }

    //额度列表返回响应
    public void onLoanLimitInfoResponse() {
        if (homeFragment != null && homeFragment instanceof LnMainFragment_boom)
            ((LnMainFragment_boom) homeFragment).updateLoanAmount_boom();
    }

    public void onDropConfigResponse(boolean showDropButton){
        if (isClose()) return;
        if (personalCenterFragment!=null) personalCenterFragment.onDropConfigResponse(showDropButton);
    }

    public void onQueryProductsSuccessful(){
        if (isClose()) return;
        if (homeFragment instanceof LnMainFragmentForOldUser_boom)
            ((LnMainFragmentForOldUser_boom)homeFragment).onListChange();
    }

    public void onCopyProcessInfoSuccessful(){
        if (isClose() || applyProduct==null) return;
        HttpManager_boom.getInstance().requestLoanApplyDetail(applyProduct.gtProduct(), applyProduct.gtToken());
    }

    public void onCopyProcessInfoFailed(){
        startProcess();
    }

}